{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# LFM算法公式推导\n",
    "\n",
    "## 建模公式\n",
    "\n",
    "$$ p(u, i) = p_u^Tq_i = \\sum_{f=1}^F p_{uf}q_{if}$$\n",
    "\n",
    "- p(u,i) 表示user item对，用户对该item有点击为1，没有点击则为0；\n",
    "\n",
    "- $p_u$表示上一节参数中的用户向量，$q_i$表示物品向量\n",
    "\n",
    "- F表示向量维度，表示用户对item喜欢与否的影响因素的个数\n",
    "\n",
    "我们用机器学习中监督学习的思想来求$p_u$和$q_i$，在F设定好之后，先使用随机数初始化，再使用梯度下降求解。\n",
    "\n",
    "## 损失函数\n",
    "\n",
    "- 对user向量的偏导，对item向量的偏导，以及user 和 item 向量的迭代。\n",
    "\n",
    "$$loss = \\sum_{(u, i) \\in D} (p(u, i) - p^{LFM}(u, i))^2$$\n",
    "\n",
    "为了防止过拟合可以增加正则化项\n",
    "\n",
    "$$loss = \\sum_{(u, i) \\in D} (p(u, i) - \\sum_{f=1}^F p_{uf}q_{if})^2 + \\alpha |p_u|^2 + \\alpha |q_i|^2$$\n",
    "\n",
    "$$\\frac{\\partial loss}{\\partial p_{uf}} = -2(p(u, i) - p^{LFM}(u, i))q_{if} + 2\\alpha p_{uf}$$\n",
    "\n",
    "$$\\frac{\\partial loss}{\\partial q_{if}} = -2(p(u, i) - p^{LFM}(u, i))p_{uf} + 2\\alpha q_{if}$$\n",
    "\n",
    "梯度下降\n",
    "\n",
    "$$p_{uf} = p_{uf} - \\beta \\frac{\\partial loss}{\\partial p_{uf}}$$\n",
    "\n",
    "$$q_{if} = q_{if} - \\beta \\frac{\\partial loss}{\\partial q_{if}}$$\n",
    "\n",
    "## 参数设定\n",
    "\n",
    "- 负样本的选取（应选取充分展现未点击的，取一定数目的）\n",
    "\n",
    "- 隐特征F （一般10到32之间）\n",
    "\n",
    "- 正则参数\n",
    "\n",
    "- 学习率\n",
    "\n",
    "## 用户对物品的兴趣\n",
    "\n",
    "$$Preference(u, i) = r_{ui} = p_u^T q_i = \\sum_{f=1}^F p_{u,k} q_{i,k}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
